#include "util.hpp"
#include "config.hpp"
#include "hot.hpp"
#include "service.hpp"
#include "hot.hpp"
#include <thread>

void FileUtilTest(const std::string &filename)
{
    // cloud::FileUtil fu(filename);

    // std::cout << fu.FileSize() << std::endl;
    // std::cout << fu.LastMTime() << std::endl;
    // std::cout << fu.LastATime() << std::endl;

    //    std::string body;
    //    fu.GetContent(&body);

    //    cloud::FileUtil nfu("./hello.txt");
    //    nfu.SetContent(body);

    // cloud::FileUtil fu(filename);
    // fu.compress(filename+".lz");

    // cloud::FileUtil pfu(filename+".lz");
    // pfu.uncompress("./hello.txt");

    cloud::FileUtil fu(filename);

    fu.CreateDirectory();

    std::vector<std::string> arry;
    fu.ScanDirectory(&arry);

    for(auto& s : arry)
    {
        std::cout << s << std:: endl;
    }

    return;
}

void JsonUtilTest()
{
    const char *name = "小明";
    int age = 19;
    float score[] = {85, 88.5, 99};

    Json::Value root;

    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);

    std::string json_str;

    cloud::JsonUtil::Serialize(root, &json_str);
    std::cout << json_str << std::endl;

    Json::Value val;
    cloud::JsonUtil::UnSerialize(json_str, &val);
    std::cout << val["姓名"].asString() << std::endl;
    std::cout << val["年龄"].asInt() << std::endl;

    for (int i = 0; i < val["成绩"].size(); i++)
    {
        std::cout << val["成绩"][i].asFloat() << std::endl;
    }

    return;
}

void ConfigTest()
{
    cloud::Config *config = cloud::Config::GetInstance();
    std::cout << config->GetHotTime() << std::endl;
    std::cout << config->GetPackDir() << std::endl;
    std::cout << config->GetBackDir() << std::endl;
    std::cout << config->GetBackupFile() << std::endl;
    std::cout << config->GetServerIp() << std::endl;
    std::cout << config->GetServerPort() << std::endl;
    std::cout << config->GetDownloadPrefix() << std::endl;
    std::cout << config->GetPackFileSuffix() << std::endl;
}

void DataTest(const std::string &filename)
{
    cloud::BackupInfo info;
    // std::cout << "-------------Info-------------" << std::endl;

    // info.NewBackupInfo(filename);
    // std::cout << info.url << std::endl;
    // std::cout << info.atime << std::endl;
    // std::cout << info.mtime << std::endl;
    // std::cout << info.pack_flag << std::endl;
    // std::cout << info.pack_path << std::endl;
    // std::cout << info.real_path << std::endl;
    // std::cout << info.fsize << std::endl;

    cloud::DataManager datamanager;
    
    // std::cout << "-------------Insert tmp-------------" << std::endl;

    // datamanager.Insert(info);

    // cloud::BackupInfo tmp;
    // datamanager.GetOneByUrl("/download/bundle.h", &tmp);
    // std::cout << tmp.url << std::endl;
    // std::cout << tmp.atime << std::endl;
    // std::cout << tmp.mtime << std::endl;
    // std::cout << tmp.pack_flag << std::endl;
    // std::cout << tmp.pack_path << std::endl;
    // std::cout << tmp.real_path << std::endl;
    // std::cout << tmp.fsize << std::endl;

    std::cout << "-------------GetAll and update-------------" << std::endl;

    //info.pack_flag = true;
    //datamanager.Update(info);

    std::vector<cloud::BackupInfo> arry;
    datamanager.GetAll(&arry);

    for (auto &a : arry)
    {
        std::cout << a.url << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.pack_flag << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.fsize << std::endl;
    }

    
    // std::cout << "-------------GetOneByRealpath-------------" << std::endl;
    // datamanager.GetOneByRealpath(filename,&tmp);
    // std::cout << tmp.url << std::endl;
    // std::cout << tmp.atime << std::endl;
    // std::cout << tmp.mtime << std::endl;
    // std::cout << tmp.pack_flag << std::endl;
    // std::cout << tmp.pack_path << std::endl;
    // std::cout << tmp.real_path << std::endl;
    // std::cout << tmp.fsize << std::endl;

}

cloud::DataManager* _data_manager;
void HotTest()
{
    cloud::HotManager hot;

    hot.RunModule();
}


void ServiceTest()
{
    cloud::Service srv;
    srv.RunModule();
}


int main(int argc, char *argv[])
{
    _data_manager = new cloud::DataManager();

    // FileUtilTest(argv[1]);
    // JsonUtilTest();
    // ConfigTest();
    // DataTest(argv[1]);
    // HotTest();
    // ServiceTest();

    std::thread thread_hot_manager(HotTest);
    std::thread thread_service(ServiceTest);

    thread_hot_manager.join();
    thread_service.join();

    return 0;
}